/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package xdxn.editor.studio.geom;

/**
 * @author Cai Han
 * @creation 2015-6-18
 */
public class Line2D {

    public Point2D from;
    public Point2D to;

    private Line2D() {
    }

    public Line2D( Point2D from , Point2D to ) {
        this.from = from;
        this.to = to;
    }

    public Line2D( double fromX , double fromY , double toX , double toY ) {
        this(new Point2D(fromX , fromY) , new Point2D(toX , toY));
    }

    public boolean pointAtLineLeft( Point2D pt  ) {
        // 设线段端点为从 A(x1, y1)到 B(x2, y2), 线外一点 P(x0，y0)，
        // 判断该点位于有向线 A→B 的那一侧。
        // a = (x2-x1, y2-y1)
        // b = (x0-x1, y0-y1)
        // a x b = | a | | b | sinφ (φ为两向量的夹角)
        // | a | | b |  ≠ 0 时，  a x b  决定点 P的位置
        // 所以  a x b  的 z 方向大小决定 P位置
        return (to.x - from.x) * (pt.y - from.y) - (to.y - from.y) * (pt.x - from.x) > 0;
    }

    public boolean pointAtLineRight( Point2D pt ) {
        return (to.x - from.x) * (pt.y - from.y) - (to.y - from.y) * (pt.x - from.x) < 0;
    }

    public boolean pointAtLine( Point2D pt ) {
        return (to.x - from.x) * (pt.y - from.y) - (to.y - from.y) * (pt.x - from.x) == 0;
    }
    
    public Vector2D getVector2D(){
        return to.sub_return(from);
    }
    
    public double getLength(){
        return Math.sqrt(getVector2D().mag());
    }
}
